﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Interface;
using Domain.Model;
using Ds.DataBase;
using Ds.DataBase.Enum;
using Ds.DataBase.Model;
using Newtonsoft.Json;

namespace Domain.RealiZation
{
    public class DeFenHuiZongtmpl : IDeFenHuiZong
    {
        public void BackDeFenHuiZong(int ksid, int pcid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var keshi = db.KeShiContext.SingleOrDefault(s => s.ksid == ksid);
                List<int> zwids = new List<int>();
                foreach (var item in keshi.keshizhiwus.Select(s=>s.zhiwu))
                {
                    zwids.Add(item.zwid);

                }

                var pch = DateTime.Now.ToString("yyyy-MM-dd");
                var defeibiaos = db.KaoHeDeFenHuiZongContext.Where(s => zwids.Contains(s.zwid) && s.pch == pch);
                foreach (var item in defeibiaos.ToArray())
                {
                    var kaohebiao = db.MyKaoHeBiaoContext.FirstOrDefault(s => s.mkhbid == item.mkhbid);
                    kaohebiao.khbzt = 考核表状态.通过审核;//TODO 这里应查询考核表日志，还原上一个状态
                    db.Entry(item).State = EntityState.Deleted;
                }

                db.SaveChanges();
            }
        }

        public void CreateDeFenBiaoHuiZong(int ksid)
        {
            //TODO 生成考核成绩
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var keshi = db.KeShiContext.SingleOrDefault(s => s.ksid == ksid);
                foreach (var item in keshi.keshizhiwus.Select(s=>s.zhiwu))
                {
                    //foreach (var item2 in item.JiaoZhiGongInfos)
                    //{
                    //    var kaohebiao = db.MyKaoHeBiaoContext.Where(s =>
                    //          s.username == item2.Users.username && s.khbzt == 考核表状态.通过审核);
                    //    foreach (var item3 in kaohebiao)
                    //    {
                    //        var defenbiaoinfo = db.KaoHeDeFenHuiZongContext.SingleOrDefault(s => s.mkhbid == item3.mkhbid);
                    //        if (defenbiaoinfo == null)
                    //        {
                    //            CreateDeFenBiao(item3.mkhbid);
                    //        }
                    //    }
                    //}
                }


            }
        }
        /// <summary>
        /// 生成得分表
        /// </summary>
        /// <param name="mkhbid"></param>
        private void CreateDeFenBiao(int mkhbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                data.khbzt = 考核表状态.考核完成;
                if (data.MyKaoHeBiaoOnes.Any(s => s.MyKaoHeBiaoTwos.Any(p => p.MyKaoHeBiaoTwoPingFens.Count > 0)))
                {

                    var pch = data.sssj.ToString("yyyy-MM-dd");
                    var userinfo = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username);
                    if (!db.KaoHeDeFenHuiZongContext.Any(s => s.pch == pch && data.mkhbid == s.mkhbid))
                    {
                        KaoHeDeFenHuiZong model = new KaoHeDeFenHuiZong();
                        model.username = data.username;
                        model.KaoHeChengJiQueRens = new List<KaoHeChengJiQueRen>();
                        model.cjsj = DateTime.Now;
                        model.fjx = data.MyKaoHeBiaoFujiaxiangs.Sum(s => s.xmfz);
                        model.KaoHeDeFenFenXiangs = new List<KaoHeDeFenFenXiang>();

                        foreach (var item in data.MyKaoHeBiaoOnes)
                        {
                            KaoHeDeFenFenXiang model2 = new KaoHeDeFenFenXiang();
                            model2.mbnr = item.mbnr;
                            model2.KaoHeDeFenHuiZongInfo = model;
                            model2.fz = 0m;
                            foreach (var item2 in item.MyKaoHeBiaoTwos)
                            {
                                decimal fz = item2.MyKaoHeBiaoTwoPingFens.Sum(s => s.fz);
                                int c = item2.MyKaoHeBiaoTwoPingFens.Count;
                                decimal f = 0m;
                                if (c > 0)
                                {
                                    f = fz / c;
                                }
                                decimal d = decimal.Round(f, 2);
                                model2.fz = model2.fz + d;
                            }
                            model2.mbnrid = item.mbnrid;
                            model2.oneid = item.oneid;
                            model2.mbtype = item.mbtype;
                            model.KaoHeDeFenFenXiangs.Add(model2);
                        }
                        model.gzjhdf = 0;
                        model.jxdf = 0;
                        var jixiaonr = data.MyKaoHeBiaoOnes.FirstOrDefault(s => s.mbtype == 2);
                        if (jixiaonr != null)
                        {
                            var mbnrid = jixiaonr.mbnrid;
                            var jixiao = db.JiXiaoContext.FirstOrDefault(s => s.JiXiaoNeiRongs.Any(p => p.ljid == mbnrid));
                            model.jxmblx = (int)jixiao.jxtype;
                            model.mbid = jixiao.mbid;
                        }
                        model.zwid = userinfo.kszw.zhiwu.zwid;
                        model.sssj = data.sssj;
                        model.pch = data.sssj.ToString("yyyy-MM-dd");//TODO 查一下批次号编码规范
                        model.dfbzt = KaoHeDeFenHuiZong.得分表状态.考核完成;
                        model.mkhbid = data.mkhbid;
                        db.KaoHeDeFenHuiZongContext.Add(model);


                    }

                }
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 删除考核得分表
        /// 需要从新确认
        /// </summary>
        /// <param name="mkhbid"></param>
        private void DelKaoHeDeFenBiao(int mkhbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.KaoHeDeFenHuiZongContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                foreach (var item in data.KaoHeDeFenFenXiangs.ToArray())
                {
                    db.Entry(item).State = EntityState.Deleted;//删除得分汇总
                }

                foreach (var item in data.KaoHeChengJiQueRens)
                {
                    db.Entry(item).State = EntityState.Deleted;//删除确认表
                }

                db.Entry(data).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
        public string GetDeFenBiao(int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var mb = db.JiXiaoContext.SingleOrDefault(s => s.mbid == mbid).JiXiaoNeiRongs.Select(p => p.nrid);
                var kaohebiaos = db.MyKaoHeBiaoContext.Where(s => s.MyKaoHeBiaoOnes.Any(p => mb.Any(t => t == p.mbnrid))).Select(w => w.mkhbid);
                var defens = db.KaoHeDeFenHuiZongContext.Where(s => kaohebiaos.Any(p => p == s.mkhbid));
                List<Domain_KaoHeDeFenHuiZong> lst = new List<Domain_KaoHeDeFenHuiZong>();
                foreach (var item in defens)
                {
                    Domain_KaoHeDeFenHuiZong model = new Domain_KaoHeDeFenHuiZong();
                    model.username = item.username;
                    model.cjsj = item.cjsj;
                    model.dfbzt = (int)item.dfbzt;
                    model.dfid = item.dfid;
                    model.fjx = item.fjx;
                    model.gzjhdf = item.gzjhdf;
                    model.jxdf = item.jxdf;
                    model.jxmblx = item.jxmblx;
                    model.jxmbid = item.mbid;
                    model.mkhbid = item.mkhbid;
                    model.KaoHeChengJiQueRens = new List<Domain_KaoHeChengJiQueRen>();
                    foreach (var item2 in item.KaoHeChengJiQueRens)
                    {
                        Domain_KaoHeChengJiQueRen model2 = new Domain_KaoHeChengJiQueRen();
                        model2.username = item2.username;
                        model2.bz = item2.bz;
                        model2.dfid = item2.dfid;
                        model2.qrid = item2.qrid;
                        model2.qrlx = (int)item2.qrlx;
                        model.KaoHeChengJiQueRens.Add(model2);
                    }
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetKaoHeWeiTiao(string username, int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                var tmp = from aa in db.KaoHeFanWeisContext where aa.username == username select new { aa.ZhiWeis };
                List<int> zwids = new List<int>();
                foreach (var item in tmp)
                {
                    zwids.AddRange(item.ZhiWeis.Select(s => s.zwid));
                }
                var defens = db.KaoHeDeFenHuiZongContext.Where(s => (mbid == 0 || mbid == s.mbid) && zwids.Contains(s.zwid) && s.dfbzt == KaoHeDeFenHuiZong.得分表状态.考核中 && s.KaoHeChengJiQueRens.All(p => p.username != username));
                if (string.IsNullOrWhiteSpace(username))
                {
                    defens = db.KaoHeDeFenHuiZongContext.Where(s => (mbid == 0 || mbid == s.mbid) && s.dfbzt == KaoHeDeFenHuiZong.得分表状态.考核中 && s.KaoHeChengJiQueRens.All(p => p.username != username));
                }
                List<Domain_KaoHeDeFenHuiZong> lst = new List<Domain_KaoHeDeFenHuiZong>();
                foreach (var item in defens)
                {
                    Domain_KaoHeDeFenHuiZong model = new Domain_KaoHeDeFenHuiZong();
                    model.username = item.username;
                    model.name = db.JiaoZhiGongInfoContext.FirstOrDefault(s => s.Users.username == item.username)
                        .XingMing;
                    model.zhiwu = db.JiaoZhiGongInfoContext.FirstOrDefault(s => s.Users.username == item.username).kszw.zhiwu
                        .zwmc;
                    model.jxmblxstr = ((JiXiao.绩效类型)item.jxmblx).ToString(); ;
                    model.cjsj = item.cjsj;
                    model.dfbzt = (int)item.dfbzt;
                    model.dfid = item.dfid;
                    model.fjx = item.fjx;
                    model.gzjhdf = item.gzjhdf;
                    model.jxdf = item.jxdf;
                    model.jxmblx = item.jxmblx;
                    model.jxmbid = item.mbid;
                    model.mkhbid = item.mkhbid;
                    model.KaoHeChengJiQueRens = new List<Domain_KaoHeChengJiQueRen>();
                    foreach (var item2 in item.KaoHeChengJiQueRens)
                    {
                        Domain_KaoHeChengJiQueRen model2 = new Domain_KaoHeChengJiQueRen();
                        model2.username = item2.username;
                        model2.bz = item2.bz;
                        model2.dfid = item2.dfid;
                        model2.qrid = item2.qrid;
                        model2.qrlx = (int)item2.qrlx;
                        model.KaoHeChengJiQueRens.Add(model2);
                    }
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }

        }

        public string GetXiaShuZhiWuYuanGong(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                //var jiaozhigongs = db.JiaoZhiGongInfoContext.FirstOrDefault(s => s.Users.username == username).zw.SonZhiWeis.SelectMany(s => s.JiaoZhiGongInfos);
                //var zhigongusernames = jiaozhigongs.Select(s => s.Users.username);
                //var defens = db.KaoHeDeFenHuiZongContext.Where(s => zhigongusernames.Contains(s.username) && s.dfbzt == KaoHeDeFenHuiZong.得分表状态.考核中 && s.KaoHeChengJiQueRens.All(p => p.username != username));
                //var re = from o in db.KaoHeDeFenHuiZongContext
                //         join d in db.JiaoZhiGongInfoContext
                //        on o.username equals d.Users.username
                //         where zhigongusernames.Contains(o.username) && o.dfbzt == KaoHeDeFenHuiZong.得分表状态.考核中 && o.KaoHeChengJiQueRens.All(p => p.username != username)
                //         select new Domain_KaoHeDeFenHuiZong()
                //         {
                //             username = o.username,
                //             cjsj = o.cjsj,
                //             dfbzt = (int)o.dfbzt,
                //             dfid = o.dfid,
                //             fjx = o.fjx,
                //             gzjhdf = o.gzjhdf,
                //             jxdf = o.jxdf,
                //             jxmblx = o.jxmblx,
                //             mkhbid = o.mkhbid
                //         };

                List<Domain_KaoHeDeFenHuiZong> lst = new List<Domain_KaoHeDeFenHuiZong>();
                //foreach (var item in defens)
                //{
                //    Domain_KaoHeDeFenHuiZong model = new Domain_KaoHeDeFenHuiZong();
                //    model.username = item.username;
                //    model.name = db.JiaoZhiGongInfoContext.FirstOrDefault(s => s.Users.username == item.username)
                //        .XingMing;
                //    model.zhiwu = db.JiaoZhiGongInfoContext.FirstOrDefault(s => s.Users.username == item.username).zw
                //        .zwmc;
                //    model.jxmblxstr = ((JiXiao.绩效类型)item.jxmblx).ToString(); ;
                //    model.cjsj = item.cjsj;
                //    model.dfbzt = (int)item.dfbzt;
                //    model.dfid = item.dfid;
                //    model.fjx = item.fjx;
                //    model.gzjhdf = item.gzjhdf;
                //    model.jxdf = item.jxdf;
                //    model.jxmblx = item.jxmblx;
                //    model.jxmbid = item.mbid;
                //    model.mkhbid = item.mkhbid;
                //    model.KaoHeChengJiQueRens = new List<Domain_KaoHeChengJiQueRen>();
                //    foreach (var item2 in item.KaoHeChengJiQueRens)
                //    {
                //        Domain_KaoHeChengJiQueRen model2 = new Domain_KaoHeChengJiQueRen();
                //        model2.username = item2.username;
                //        model2.bz = item2.bz;
                //        model2.dfid = item2.dfid;
                //        model2.qrid = item2.qrid;
                //        model2.qrlx = (int)item2.qrlx;
                //        model.KaoHeChengJiQueRens.Add(model2);
                //    }
                //    lst.Add(model);
                //}
                return JsonConvert.SerializeObject(lst);
            }
        }

        public void QueRenXiaShuDeFen(string username)
        {
            //using (UserInfoClassContext db = new UserInfoClassContext())
            //{
            //    var jiaozhigongs = db.JiaoZhiGongInfoContext.FirstOrDefault(s => s.Users.username == username)
            //        ?.zw
            //        .SonZhiWeis.SelectMany(s => s.JiaoZhiGongInfos);
            //    var zhigongusernames = jiaozhigongs.Select(s => s.Users.username);
            //    var defens = db.KaoHeDeFenHuiZongContext.Where(s =>
            //        zhigongusernames.Contains(s.username) && s.dfbzt == KaoHeDeFenHuiZong.得分表状态.考核中 &&
            //        s.KaoHeChengJiQueRens.All(p => p.username != username));
            //    foreach (var item in defens)
            //    {
            //        item.KaoHeChengJiQueRens.Add(new KaoHeChengJiQueRen() { username = username, bz = "", qrlx = KaoHeChengJiQueRen.确认类型.同意, dfid = item.dfid, KaoHeDeFenHuiZong = item });
            //    }
            //    db.SaveChanges();
            //}
        }

        public void UpdateDeFenBiaoHuiZong(int mkhbid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var item3 = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                KaoHeDeFenHuiZong model = db.KaoHeDeFenHuiZongContext.SingleOrDefault(s => s.mkhbid == item3.mkhbid);//一个考核表对应一个得分表
                model.username = item3.username;
                if (model.KaoHeChengJiQueRens.All(s => s.username != username))
                {
                    model.fjx = item3.MyKaoHeBiaoFujiaxiangs.Sum(s => s.xmfz);
                }

                db.SaveChanges();
                //foreach (var item in model.KaoHeChengJiQueRens.ToList())
                //{
                //    db.Entry(item).State = EntityState.Deleted;
                //}
                //model.KaoHeChengJiQueRens = new List<KaoHeChengJiQueRen>();
                //model.cjsj = DateTime.Now;

                //model.gzjhdf = item3.MyKaoHeBiaoOnes.Where(s => s.mbtype == 1)
                //    .Sum(p => p.MyKaoHeBiaoTwos.Sum(t => t.fz));
                //model.jxdf = item3.MyKaoHeBiaoOnes.Where(s => s.mbtype == 2)
                //    .Sum(p => p.MyKaoHeBiaoTwos.Sum(t => t.fz));
                //var mbnrid = item3.MyKaoHeBiaoOnes.FirstOrDefault(s => s.mbtype == 2).mbnrid;
                //var jixiao = db.JiXiaoContext.FirstOrDefault(s => s.JiXiaoNeiRongs.Any(p => p.nrid == mbnrid));
                //model.jxmblx = (int)jixiao.jxtype;
                //model.mkhbid = item3.mkhbid;
            }
        }
    }
}
